<?

class Image{ 

    var $src_filename; 

    var $src_width;

    var $src_height; 

    var $src_type; 

    var $src_attr; 

    var $src_image; 
	
	var $name; 



    function Image($filename){ 

        $this->src_filename = $filename; 

        $this->GetImageInfo();
        
        $this->CreateSourceImage();

    } 



    function GetImageInfo(){ 

        list($this->src_width,$this->src_height, $this->src_type, $this->src_attr) = getimagesize($this->src_filename); 

    } 



    function CreateSourceImage(){ 

        switch($this->src_type){ 

            case 1: 

                $this->src_image =imagecreatefromgif($this->src_filename); 

                   break; 

            case 2: 

                $this->src_image =imagecreatefromjpeg($this->src_filename); 

            break; 

            case 3: 

                $this->src_image =imagecreatefrompng($this->src_filename); 

            break; 

            default:    return false; 

        } 



        return true; 

    } 



    function SaveProportionateImage($filename, $quality, $height, $width){
      if($height=="0")  {
		$dest_width = $width;
        $ratio = $this->src_width / $width;
        $dest_height = $this->src_height / $ratio;
	   } else {
		   $dest_height = $height;
           $ratio = $this->src_height / $height;
           $dest_width = $this->src_width / $ratio;
	   }
		/*
        $dest_image = imagecreatetruecolor($dest_width,$dest_height);

        imagecopyresampled($dest_image, $this->src_image, 0, 0, 0, 0, 

            $dest_width,

            $dest_height,

            $this->src_width, 

            $this->src_height); 
		*/
	    $dest_image = Image::CroppedThumbnail($this->src_filename, $dest_width, $dest_width);
        switch($this->src_type){ 

            case IMG_GIF:
            if(function_exists(imagegif))
             imagegif($dest_image, $filename, $quality);
            break;

            case IMG_JPG:
            if(function_exists(imagejpeg)) {
             //die("Errore!!!");
             imagejpeg($dest_image, $filename, $quality); }
            break;

            case IMG_PNG:
            if(function_exists(imagepng))
             imagepng($dest_image, $filename, $quality);
            break;
            
            case IMG_WBMP:
            if(function_exists(imagewbmp))
             imagewbmp($dest_image, $filename, $quality);
            break;

            default:
             return false;
            break;

        }
         
        imagedestroy($dest_image);
		return true;
    }

    function CroppedThumbnail($imgSrc,$thumbnail_width,$thumbnail_height) {
    	//$imgSrc is a FILE - Returns an image resource.
    	//getting the image dimensions
    	list($width_orig, $height_orig) = getimagesize($imgSrc);
    	$myImage = imagecreatefromjpeg($imgSrc);
    	$ratio_orig = $width_orig/$height_orig;
    	 
    	if ($thumbnail_width/$thumbnail_height > $ratio_orig) {
    		$new_height = $thumbnail_width/$ratio_orig;
    		$new_width = $thumbnail_width;
    	} else {
    		$new_width = $thumbnail_height*$ratio_orig;
    		$new_height = $thumbnail_height;
    	}
    	 
    	$x_mid = $new_width/2;  //horizontal middle
    	$y_mid = $new_height/2; //vertical middle
    	 
    	$process = imagecreatetruecolor(round($new_width), round($new_height));
    	 
    	imagecopyresampled($process, $myImage, 0, 0, 0, 0, $new_width, $new_height, $width_orig, $height_orig);
    	$thumb = imagecreatetruecolor($thumbnail_width, $thumbnail_height);
    	imagecopyresampled($thumb, $process, 0, 0, ($x_mid-($thumbnail_width/2)), ($y_mid-($thumbnail_height/2)), $thumbnail_width, $thumbnail_height, $thumbnail_width, $thumbnail_height);
    
    	imagedestroy($process);
    	imagedestroy($myImage);
    	return $thumb;
    }
    
	function SaveProportionateImage2($filename, $quality, $height, $width){
        if($height=="0")  {
        $dest_width = $width;
        $ratio = $this->src_width / $width;
        $dest_height = $this->src_height / $ratio;
        } else {
        	$dest_height = $height;
        	$ratio = $this->src_height / $height;
        	$dest_width = $this->src_width / $ratio;
        }
        $dest_image = imagecreatetruecolor($dest_width,$dest_height);
        imagecopyresampled($dest_image, $this->src_image, 0, 0, 0, 0, $dest_width,$dest_height,$this->src_width,$this->src_height); 

        switch($this->src_type){ 

            case 1:
            if(function_exists(imagegif)) {
             imagegif($dest_image, $filename, $quality);
             chmod($filename,0755);
            }
            break;

            case 2:
            if(function_exists(imagejpeg)) {
             //die("Errore!!!");
             imagejpeg($dest_image, $filename, $quality); 
             chmod($filename,0755);
            }
            break;

            case 3:
            if(function_exists(imagepng)) {
             imagepng($dest_image, $filename, $quality);
             chmod($filename,0755);
            }
            break;

            default:
             return false;
            break;

        }
         
        imagedestroy($dest_image);

    }



    function Free(){ 

        imagedestroy($this->src_image); 

    }
    
    function CheckSizeFormat($file,$width,$height,$tipo="") {
        global $path_tmp;
		  if($this->src_width <= $width && $this->src_height <= $height) {
		    $this->name = $file["name"];
		    //faccio l'upload in tmp
		    $newFile = new FileUpload($path_tmp);
		    $newFile->Upload($file);
        	$error = "";
		  } else
		     $error = "Controlla la dimensione dell'immagine $tipo.<br>";  
	  return $error;  
	}

} 
?>